<?php
set_time_limit(0);
include("class.base.php");
if (!$sess = sisse_logitud()) { header("Location: index.php"); die(); }
if (isset($_GET["path"])) {
	$_GET["path"] = str_replace("..", "", $_GET["path"]);
	if (!strstr($_GET["path"], 'KASIKIRI_PATH/')) {
		header("HTTP/1.0 404 Not Found"); die();
	}
	include("conf.php");
	$fullImagePath = str_replace('KASIKIRI_PATH/', KASIKIRI_PATH, $_GET["path"]);
	$originalFileName = array_reverse(explode("/", $fullImagePath));
	$newestFileTime = 0;
	$handler = opendir($fullImagePath);
	while ($file = readdir($handler)) {
		if ($file != "." and $file != "..") {
			$isitimage = array_reverse(explode(".", $file));
			if ($isitimage[0] == "JPG" or $isitimage[0] == "jpg" or $isitimage[0] == "PDF" or $isitimage[0] == "pdf") {
				$fileTime = filemtime($fullImagePath."/".$file);
				if ($fileTime > $newestFileTime) {
					$newestFileTime = $fileTime;
				}
			}
		}
	}
	closedir($handler);
	if (!file_exists('zip_archive/'.$originalFileName[0].'.zip') || $newestFileTime > filemtime('zip_archive/'.$originalFileName[0].'.zip')) {
		$zip = new ZipArchive();
		$zip->open('zip_archive/'.$originalFileName[0].'.zip', ZIPARCHIVE::CREATE);
		$handler = opendir($fullImagePath);
		while ($file = readdir($handler)) {
			if ($file != "." and $file != "..") {
				$isitimage = array_reverse(explode(".", $file));
				if ($isitimage[0] == "JPG" or $isitimage[0] == "jpg" or $isitimage[0] == "PDF" or $isitimage[0] == "pdf") {
					$zip->addFile($fullImagePath."/".$file, $originalFileName[0]."/".$file);
				}
			}
		}
		closedir($handler);
		$zip->close();
	}
	else { exec("touch zip_archive/".$originalFileName[0].".zip"); }
	header("Location: zip_archive/".$originalFileName[0].".zip");
}
?>